package main

import (
	"homeworks/ch3/unsafe_usage/poid/p"
	"unsafe"
)

//unsafe.pointer用于访问操作结构体的私有变量
//https://www.cnblogs.com/-wenli/p/12682477.html
func main() {
	var v = new(p.V)
	v.PrintI()
	v.PrintJ()

	var i = (*int32)(unsafe.Pointer(v))
	*i = int32(98)
	v.PrintI()
	v.PrintJ()
	println("unsafe.Sizeof(int32(0))):", unsafe.Sizeof(int32(0)))
	println("unsafe.Alignof(int32(0))):", unsafe.Alignof(int32(0)))
	var j = (*int64)(unsafe.Pointer(uintptr(unsafe.Pointer(v)) + (unsafe.Sizeof(int32(0)))))
	*j = int64(763)
	v.PrintI()
	v.PrintJ()

	j = (*int64)(unsafe.Pointer(uintptr(unsafe.Pointer(v)) + uintptr(8)))
	*j = int64(763)
	v.PrintI()
	v.PrintJ()

	println("unsafe.Offsetof(v.J):", unsafe.Offsetof(v.J))
	j = (*int64)(unsafe.Pointer(uintptr(unsafe.Pointer(v)) + unsafe.Offsetof(v.J)))
	*j = int64(1024)
	v.PrintI()
	v.PrintJ()
}
